﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package reprise.core {
    import reprise.ui.*;
    import flash.events.*;
    import reprise.commands.*;
    import reprise.events.*;
    import flash.display.*;
    import reprise.utils.*;
    import reprise.ui.renderers.*;

    public class TooltipManager {

        protected var m_tooltipContainer:Sprite;
        protected var m_tooltipDelay:Delegate;
        protected var m_lastMousedElement:DisplayObject;
        protected var m_lastTooltipDataProvider:UIObject;
        protected var m_tooltipState:uint = 4
        protected var m_tooltip:AbstractTooltip;
        protected var m_rootView:DocumentView;

        protected static const TOOLTIP_INVISIBLE:uint = 4;
        protected static const TOOLTIP_HIDE_ANIMATION:uint = 3;
        protected static const TOOLTIP_VISIBLE:uint = 2;
        protected static const TOOLTIP_SHOW_ANIMATION:uint = 1;

        public function TooltipManager(_arg1:DocumentView, _arg2:Sprite){
            m_rootView = _arg1;
            m_rootView.addEventListener(MouseEvent.MOUSE_OVER, rootView_mouseOver);
            m_rootView.addEventListener(MouseEvent.MOUSE_OUT, rootView_mouseOut);
            m_tooltipContainer = _arg2;
            m_tooltipContainer.mouseEnabled = false;
            m_tooltipContainer.mouseChildren = false;
        }
        protected function rootView_mouseOver(_arg1:MouseEvent):void{
            updateTooltipForElement(DisplayObject(_arg1.target));
        }
        protected function updateTooltipPosition():void{
            if (!m_tooltip){
                return;
            };
            m_tooltip.updatePosition();
        }
        protected function rootView_mouseOut(_arg1:MouseEvent):void{
            hideTooltip();
        }
        protected function mousedElement_tooltipDataChanged(_arg1:DisplayEvent):void{
            updateTooltipForElement(m_lastMousedElement);
        }
        protected function updateTooltipForElement(_arg1:DisplayObject):void{
            var _local2:UIObject = findTooltipDataProviderForElement(_arg1);
            var _local3:Object = (m_tooltip) ? m_tooltip.data() : null;
            var _local4:Object = (_local2) ? _local2.tooltipData() : null;
            if ((((_local2 == m_lastTooltipDataProvider)) && ((_local3 == _local4)))){
                if ((((((m_tooltipState == TOOLTIP_HIDE_ANIMATION)) || ((m_tooltipState == TOOLTIP_INVISIBLE)))) && (!((_local4 == null))))){
                    m_tooltip.show();
                    m_tooltipState = TOOLTIP_SHOW_ANIMATION;
                };
                return;
            };
            if (m_lastTooltipDataProvider){
                m_lastTooltipDataProvider.removeEventListener(DisplayEvent.TOOLTIPDATA_CHANGED, mousedElement_tooltipDataChanged);
            };
            if (_local2){
                _local2.addEventListener(DisplayEvent.TOOLTIPDATA_CHANGED, mousedElement_tooltipDataChanged);
            };
            m_lastMousedElement = _arg1;
            m_lastTooltipDataProvider = _local2;
            if ((((_local2 == null)) && (m_tooltip))){
                m_tooltip.setMousedElement(null);
                m_tooltip.setTooltipDataProvider(null);
                m_tooltip.setMousedComponent(null);
            };
            if ((((_local2 == null)) || ((_local2.tooltipData() == null)))){
                hideTooltip();
                return;
            };
            var _local5:Object = _local2.tooltipData();
            var _local6:String = _local2.tooltipRenderer();
            if (m_tooltipDelay != null){
                TimeCommandExecutor.instance().removeCommand(m_tooltipDelay);
                m_tooltipDelay = null;
            };
            if ((((_local2.tooltipDelay() > 0)) && ((m_tooltipState == TOOLTIP_INVISIBLE)))){
                m_tooltipDelay = new Delegate(this, showTooltipWithDataAndRendererForElement, [_local5, _local6, _arg1, _local2]);
                TimeCommandExecutor.instance().delayCommand(m_tooltipDelay, _local2.tooltipDelay());
            } else {
                showTooltipWithDataAndRendererForElement(_local5, _local6, _arg1, _local2);
            };
        }
        protected function rootView_enterFrame(_arg1:Event):void{
            updateTooltipPosition();
        }
        protected function tooltip_hideComplete(_arg1:DisplayEvent):void{
            removeTooltip(AbstractTooltip(_arg1.target));
        }
        protected function tooltip_showComplete(_arg1:DisplayEvent):void{
            m_tooltipState = TOOLTIP_VISIBLE;
        }
        protected function removeTooltip(_arg1:AbstractTooltip=null):void{
            if (!_arg1){
                _arg1 = m_tooltip;
            };
            if (!_arg1){
                return;
            };
            _arg1.removeEventListener(DisplayEvent.HIDE_COMPLETE, tooltip_hideComplete);
            _arg1.removeEventListener(DisplayEvent.SHOW_COMPLETE, tooltip_showComplete);
            _arg1.remove();
            m_tooltipState = TOOLTIP_INVISIBLE;
            if (_arg1 == m_tooltip){
                m_rootView.removeEventListener(Event.ENTER_FRAME, rootView_enterFrame);
                m_tooltip = null;
            };
        }
        protected function hideTooltip():void{
            if ((((m_tooltipState == TOOLTIP_INVISIBLE)) || ((m_tooltipState == TOOLTIP_HIDE_ANIMATION)))){
                return;
            };
            if (m_tooltipDelay != null){
                TimeCommandExecutor.instance().removeCommand(m_tooltipDelay);
                m_tooltipDelay = null;
            };
            m_tooltipState = TOOLTIP_HIDE_ANIMATION;
            m_tooltip.hide();
        }
        protected function showTooltipWithDataAndRendererForElement(_arg1:Object, _arg2:String, _arg3:DisplayObject, _arg4:UIObject, _arg5:Delegate=null):void{
            var _local6:String = ((_arg2 == null)) ? "default" : _arg2;
            if ((((m_tooltip == null)) || (!(m_tooltip.hasCSSClass(_local6))))){
                removeTooltip();
                m_tooltip = _arg4.document.uiRendererFactory().tooltipRendererById(_arg2);
                m_tooltipContainer.addChild(m_tooltip);
                m_tooltip.setParent(_arg4.document);
                m_tooltip.cssID = "Tooltip";
                m_tooltip.mouseEnabled = (m_tooltip.mouseChildren = false);
                m_tooltip.addEventListener(DisplayEvent.SHOW_COMPLETE, tooltip_showComplete);
                m_tooltip.addEventListener(DisplayEvent.HIDE_COMPLETE, tooltip_hideComplete);
                m_rootView.addEventListener(Event.ENTER_FRAME, rootView_enterFrame);
            };
            m_tooltip.setTooltipDataProvider(_arg4);
            m_tooltip.setMousedElement(_arg3);
            m_tooltip.setMousedComponent(findComponentForMousedElement(_arg3));
            m_tooltip.addCSSClass(_local6);
            m_tooltip.setData(_arg1);
            m_tooltip.forceRedraw();
            updateTooltipPosition();
            if (((!((m_tooltipState == TOOLTIP_VISIBLE))) && (!((m_tooltipState == TOOLTIP_SHOW_ANIMATION))))){
                m_tooltip.show();
                m_tooltipState = TOOLTIP_SHOW_ANIMATION;
            };
        }
        protected function findTooltipDataProviderForElement(_arg1:DisplayObject):UIObject{
            while (_arg1 != null) {
                if ((((_arg1 is UIObject)) && (!((UIObject(_arg1).tooltipData() == null))))){
                    return (UIObject(_arg1));
                };
                _arg1 = _arg1.parent;
            };
            return (null);
        }
        protected function findComponentForMousedElement(_arg1:DisplayObject):UIComponent{
            while (_arg1 != null) {
                if ((_arg1 is UIComponent)){
                    return (UIComponent(_arg1));
                };
                _arg1 = _arg1.parent;
            };
            return (null);
        }

    }
}//package reprise.core 
